package team.kirohuji.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import team.kirohuji.dao.PriorityDao;
import team.kirohuji.dao.SystemRolePriorityDao;
import team.kirohuji.service.SystemRolePriorityService;

/**
 * 系统 角色-权限 业务类
 * 
 * @author kirohuji
 *
 */
@Service("systemRolePriorityService")
public class SystemRolePriorityServiceImpl implements SystemRolePriorityService {

	/**
	 * @see SystemRolePriorityDao systemRolePriorityDao
	 */
	@Resource
	private SystemRolePriorityDao systemRolePriorityDao;
	/**
	 * @see PriorityDao priorityDao
	 */
	@Resource
	private PriorityDao priorityDao;

	/*
	 * 保存如果存在则更新 (non-Javadoc)
	 * 
	 * @see team.kirohuji.service.SystemRolePriorityService#saveOrUpdate(java.lang.
	 * Integer, java.util.List)
	 * 
	 * @param roleId 角色主键
	 * 
	 * @param menuIdList 菜单主键列表
	 */
	@Override
	public void saveOrUpdate(Integer roleId, List<Integer> menuIdList) {

	}

	/*
	 * 查询菜单列表通过角色Id (non-Javadoc)
	 * 
	 * @see
	 * team.kirohuji.service.SystemRolePriorityService#queryMenuIdList(java.lang.
	 * Integer)
	 * 
	 * @param roleId 角色Id
	 * 
	 * @return Integer列表
	 */
	@Override
	public List<Integer> queryMenuIdList(Integer roleId) {
		return null;
	}

	@Override
	public boolean modifySystemRolePriority(Integer roleId, String priortiy1, String priortiy2, String priortiy3,
			String priortiy4, String priortiy5, String priortiy6, String priortiy7) {
		List<Integer> priorities = systemRolePriorityDao.queryMenuIdList(roleId);

		if (priorities.contains(1) == true && "false".equals(priortiy1)) {
			systemRolePriorityDao.deleteNodePriority(roleId, 1);
			systemRolePriorityDao.deleteParentPriority(roleId, 1);
		} else if (priorities.contains(1) == false && "true".equals(priortiy1)) {
			systemRolePriorityDao.insertParentPriority(roleId, 1);
			List<Integer> nodePriorities = priorityDao.queryNodePriority(1);
			systemRolePriorityDao.insertNodePriority(roleId, nodePriorities);
		}

		if (priorities.contains(2) == true && "false".equals(priortiy2)) {
			systemRolePriorityDao.deleteNodePriority(roleId, 2);
			systemRolePriorityDao.deleteParentPriority(roleId, 2);
		} else if (priorities.contains(2) == false && "true".equals(priortiy2)) {
			systemRolePriorityDao.insertParentPriority(roleId, 2);
			List<Integer> nodePriorities = priorityDao.queryNodePriority(2);
			systemRolePriorityDao.insertNodePriority(roleId, nodePriorities);
		}

		if (priorities.contains(3) == true && "false".equals(priortiy3)) {
			systemRolePriorityDao.deleteNodePriority(roleId, 3);
			systemRolePriorityDao.deleteParentPriority(roleId, 3);
		} else if (priorities.contains(3) == false && "true".equals(priortiy3)) {
			systemRolePriorityDao.insertParentPriority(roleId, 3);
			List<Integer> nodePriorities = priorityDao.queryNodePriority(3);
			systemRolePriorityDao.insertNodePriority(roleId, nodePriorities);
		}

		if (priorities.contains(4) == true && "false".equals(priortiy4)) {
			systemRolePriorityDao.deleteNodePriority(roleId, 4);
			systemRolePriorityDao.deleteParentPriority(roleId, 4);
		} else if (priorities.contains(4) == false && "true".equals(priortiy4)) {
			systemRolePriorityDao.insertParentPriority(roleId, 4);
			List<Integer> nodePriorities = priorityDao.queryNodePriority(4);
			systemRolePriorityDao.insertNodePriority(roleId, nodePriorities);
		}

		if (priorities.contains(5) == true && "false".equals(priortiy5)) {
			systemRolePriorityDao.deleteNodePriority(roleId, 5);
			systemRolePriorityDao.deleteParentPriority(roleId, 5);
		} else if (priorities.contains(5) == false && "true".equals(priortiy5)) {
			systemRolePriorityDao.insertParentPriority(roleId, 5);
			List<Integer> nodePriorities = priorityDao.queryNodePriority(5);
			systemRolePriorityDao.insertNodePriority(roleId, nodePriorities);
		}

		if (priorities.contains(6) == true && "false".equals(priortiy6)) {
			systemRolePriorityDao.deleteNodePriority(roleId, 6);
			systemRolePriorityDao.deleteParentPriority(roleId, 6);
		} else if (priorities.contains(6) == false && "true".equals(priortiy6)) {
			systemRolePriorityDao.insertParentPriority(roleId, 6);
			List<Integer> nodePriorities = priorityDao.queryNodePriority(6);
			systemRolePriorityDao.insertNodePriority(roleId, nodePriorities);
		}

		if (priorities.contains(7) == true && "false".equals(priortiy7)) {
			systemRolePriorityDao.deleteNodePriority(roleId, 7);
			systemRolePriorityDao.deleteParentPriority(roleId, 7);
		} else if (priorities.contains(7) == false && "true".equals(priortiy7)) {
			systemRolePriorityDao.insertParentPriority(roleId, 7);
			List<Integer> nodePriorities = priorityDao.queryNodePriority(7);
			systemRolePriorityDao.insertNodePriority(roleId, nodePriorities);
		}

		return true;
	}
}
